package com.styytech.dadabao.ui.project;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.styytech.dadabao.DadaApplication;
import com.styytech.dadabao.R;
import com.styytech.dadabao.base.BaseActivity;
import com.styytech.dadabao.bean.RecommendProduct;
import com.styytech.dadabao.constant.ConstantsCommon;
import com.styytech.dadabao.constant.ConstantsServerUrl;

import java.util.List;


@ContentView(R.layout.activity_more_details)
public class MoreDetailsActivity extends BaseActivity {


    public final static int ID_DESC = R.id.tv_desc;
    public final static int ID_RECORD = R.id.tv_myRecord;


    @ViewInject(R.id.tl_record_top)
    private LinearLayout tl_record_top;

    @ViewInject(R.id.tv_desc)
    private TextView tv_desc;
    @ViewInject(R.id.tv_myRecord)
    private TextView tv_myRecord;


    /**
     * 记录点击item的Id
     */
    private int itemId = 0;
    private MoreDetailDescriptionFragment descriptionFragment = null;
    private MoreDetailRecordFragment recordFragment = null;

    /**
     * 用于对Fragment进行管理
     */
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    RecommendProduct recommendProduct = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
    }

    private void initData() {
        context = this;
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        recommendProduct = (RecommendProduct) bundle.getSerializable("recommendProduct");
        fragmentManager = getSupportFragmentManager();
    }

    /**
     * 初始化界面
     */
    private void initView() {
        DadaApplication.addActivity(this);
        setTopView();
        initFragment();
    }

    private void setTopView() {
        setTopBar(null, View.VISIBLE, View.VISIBLE, resources.getString(R.string.common_details));
    }

    /*
    * 设置Fragment，显示主界面
    */
    private void initFragment() {
        //默认页
        itemId = ID_DESC;
        changeStatus(itemId);
        setTabSelection(itemId);// 第一次启动时选中第0个tab
    }

    /**
     * 更换图片/修改字体颜色/背景颜色
     */
    private void changeStatus(int itemId) {
        /**
         * 描述
         */
        if (itemId == ID_DESC) {
            tv_desc.setBackgroundResource(R.drawable.shape_text_radius_left_selected);
            tv_desc.setTextColor(resources.getColor(R.color.main));
        } else {
            tv_desc.setBackgroundResource(R.drawable.shape_text_radius_left_nomal);
            tv_desc.setTextColor(resources.getColor(R.color.white));
        }

        /**
         * 投资记录
         */
        if (itemId == ID_RECORD) {
            tv_myRecord.setBackgroundResource(R.drawable.shape_text_radius_right_selected);
            tv_myRecord.setTextColor(resources.getColor(R.color.main));
        } else {
            tv_myRecord.setBackgroundResource(R.drawable.shape_text_radius_right_nomal);
            tv_myRecord.setTextColor(resources.getColor(R.color.white));
        }
    }


    @OnClick({ID_DESC, ID_RECORD})
    public void onFragmentClick(View view) {
        if (itemId == view.getId()) {
            return;
        } else {
            itemId = view.getId();
        }
        changeStatus(itemId);// 修改item状态
        setTabSelection(itemId);
    }

    private void setTabSelection(int index) {
        // 开启一个Fragment事务
        transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case ID_DESC:
                tl_record_top.setVisibility(View.GONE);
                descriptionFragment = (MoreDetailDescriptionFragment) fragmentManager
                        .findFragmentByTag("ID_DESC");
                if (descriptionFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    descriptionFragment = MoreDetailDescriptionFragment.newInstance(recommendProduct);
                    transaction.add(R.id.fl_content, descriptionFragment, "ID_DESC");
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(descriptionFragment);
                }
                break;
            case ID_RECORD:
                tl_record_top.setVisibility(View.VISIBLE);
                recordFragment = (MoreDetailRecordFragment) fragmentManager
                        .findFragmentByTag("ID_RECORD");
                if (recordFragment == null) {
                    // 如果MessageFragment为空，则创建一个并添加到界面上
                    String newsUrl = ConstantsServerUrl.getProductSaleRecord();
                    recordFragment = MoreDetailRecordFragment.newInstance(newsUrl,
                            ConstantsCommon.DATA_SRC_RECORD,
                            ConstantsCommon.DATA_SRC_RECORD, recommendProduct.getId());
                    transaction.add(R.id.fl_content, recordFragment, "ID_RECORD");
                } else {
                    // 如果MessageFragment不为空，则直接将它显示出来
                    transaction.show(recordFragment);
                }
                break;
        }
        transaction.commit();
    }

    private void hideFragments(FragmentTransaction transaction) {
        List<Fragment> fList = fragmentManager.getFragments();
        if (fList == null) {
            return;
        }
        for (Fragment fragment : fList) {
            if (fragment != null) {
                transaction.hide(fragment);
            }
        }
    }
}
